package com.hua.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hua.lease.common.constant.RedisConstant;
import com.hua.lease.common.exception.LeaseException;
import com.hua.lease.common.result.ResultCodeEnum;
import com.hua.lease.common.utils.JwtUtil;
import com.hua.lease.model.entity.SystemUser;
import com.hua.lease.model.enums.BaseStatus;
import com.hua.lease.web.admin.mapper.SystemUserMapper;
import com.hua.lease.web.admin.service.LoginService;
import com.hua.lease.web.admin.vo.login.CaptchaVo;
import com.hua.lease.web.admin.vo.login.LoginVo;
import com.hua.lease.web.admin.vo.system.user.SystemUserInfoVo;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
public class LoginServiceImpl implements LoginService {

	@Autowired
	private StringRedisTemplate redisTemplate;

	@Autowired
	private SystemUserMapper systemUserMapper;

	@Override
	public CaptchaVo getCaptcha() {
		//1. 使用SpecCaptcha类生成一个指定尺寸和字符数量的验证码图片对象。
		SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);
		//2. 设置验证码字体类型为默认类型
		specCaptcha.setCharType(Captcha.TYPE_DEFAULT);
		//3.将生成的验证码文本转换为小写字母格式。
		String code = specCaptcha.text().toLowerCase();
		//4.生成一个唯一的键，用于在Redis中存储验证码。
		String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID();
		//5.将验证码文本转换为Base64编码格式
		String image = specCaptcha.toBase64();
		//6.使用Redis数据库，将验证码键和值进行关联，并设置过期时间。
		redisTemplate.opsForValue().set(key, code, RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);
		//7.将验证码图片的Base64字符串和键封装到CaptchaVo对象中，并返回该对象。
		return new CaptchaVo(image, key);
	}

	@Override
	public String login(LoginVo loginVo) {
		//1. 校验验证码是否为空
		if (loginVo.getCaptchaCode() == null || loginVo.getCaptchaCode().isEmpty()) {
			throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
		}
		//2. 校验验证码是否超时
		String code = redisTemplate.opsForValue().get(loginVo.getCaptchaKey());
		if (code == null || code.isEmpty()) {
			throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
		}
		//3. 校验验证码是否正确
		if (!code.equals(loginVo.getCaptchaCode().toLowerCase())) {
			throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
		}
		//4.查询用户信息，校验用户状态
		SystemUser systemUser = systemUserMapper.selectOneByUsername(loginVo.getUsername());
		if (systemUser == null) {
			throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
		}
		if (systemUser.getStatus() == BaseStatus.DISABLE) {
			throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
		}
		//5.校验密码是否正确
		if (!systemUser.getPassword().equals(DigestUtils.md5Hex(loginVo.getPassword()))) {
			throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
		}
		//6.创建并返回TOKEN
		return JwtUtil.createToken(systemUser.getId(), systemUser.getUsername());
	}

	@Override
	public SystemUserInfoVo getLoginUserInfoById(Long userId) {
		SystemUser systemUser = systemUserMapper.selectById(userId);
		if (systemUser == null) {
			throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
		}
		SystemUserInfoVo systemUserInfoVo = new SystemUserInfoVo();
		systemUserInfoVo.setAvatarUrl(systemUser.getAvatarUrl());
		systemUserInfoVo.setName(systemUser.getName());
		return systemUserInfoVo;


	}

	@Override
	public SystemUserInfoVo getLoginUserInfo(Long userId) {
		SystemUser systemUser = systemUserMapper.selectById(userId);
		SystemUserInfoVo systemUserInfoVo = new SystemUserInfoVo();
		systemUserInfoVo.setName(systemUser.getName());
		systemUserInfoVo.setAvatarUrl(systemUser.getAvatarUrl());
		return systemUserInfoVo;
	}
}
